package part2;

import java.util.Scanner;

public class part2_2 {
    public static void main(String[] args) {
        //java数组初始化
        String[] strArray={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
        String[][] str=new String[16][16];
        for(int i=0;i<16;i++){
            str[0][i]=strArray[i];
            str[i][0]=strArray[i];
        }
        for (int i=1;i<16;i++){
            for(int j=1;j<16;j++){
                str[i][j]="+";
            }
        }
        game:while (true){
            for (int i=0;i<16;i++){
                for(int j=0;j<16;j++){
                    System.out.print(str[i][j]+" ");
                }
                System.out.println(" ");
            }
            //下子
            Scanner input = new Scanner(System.in);
            int[] blackPoint = new int[2];        //建立一个数组存放黑方下子坐标
            int[] whitePoint = new int[2];        //建立一个数组存放白方下子坐标
            boolean v = true;

            while(v){
                System.out.println("请黑方下子（请输入下子的坐标）：");
                for(int i = 0;i<blackPoint.length;i++) {
                    blackPoint[i] = input.nextInt();
                }
                if(str[blackPoint[1]][blackPoint[0]]=="+") {
                    str[blackPoint[1]][blackPoint[0]] = "黑";
                    //在此处插入判断条件
                    v = victory(str,blackPoint[0],blackPoint[1]);
                    if(v==false) {
                        System.out.println("黑方胜利！！");
                        break game;
                    }
                    break;
                }else if(blackPoint[1]==0 || blackPoint[0]==0 || blackPoint[1]>=str.length || blackPoint[0]>=str.length){
                    System.out.println("您的坐标输入有误！请重新输入！");
                    continue;
                }else {
                    System.out.println("该位置已有子！请重新输入！");
                    continue;
                }
            }


            for(int i=0;i<str.length;i++) {   		//黑方下子后打印一次
                for(int j=0;j<str.length;j++) {
                    System.out.print(str[i][j]+"	");
                }
                System.out.println();
                //System.out.println();
            }

            while(v) {
                System.out.println("请白方下子（请输入下子的坐标）：");
                for(int i = 0;i<whitePoint.length;i++) {
                    whitePoint[i] = input.nextInt();
                }
                if(str[whitePoint[1]][whitePoint[0]]=="+") {
                    str[whitePoint[1]][whitePoint[0]] = "白";
                    //在此处插入判断条件
                    v = victory(str,whitePoint[0],whitePoint[1]);
                    if(v==false) {
                        System.out.println("白方胜利！！");
                        break game;
                    }
                    break;
                }else if(whitePoint[1]==0 || whitePoint[0]==0 || whitePoint[1]>=str.length || whitePoint[0]>=str.length){
                    System.out.println("您的坐标输入有误！请重新输入！");
                    continue;
                }else {
                    System.out.println("该位置已有子！请重新输入！");
                    continue;
                }
            }
        }








    }

    public static boolean victory(String[][] str, int point1,int point2){
        //判断落子的邻近是否可以有形成的五个连续的子
        //如果有即放回false给v;
        int leftNumber = 0;        	 	//定义落子的左边连续相同的个数
        int rightNumber = 0;			//定义落子的右边连续相同的个数
        int upperNumber = 0;			//定义落子的上边连续相同的个数
        int lowerNumber = 0;			//定义落子的下边连续相同的个数
        int leftUpperNumber = 0;		//定义落子的左上连续相同的个数
        int leftLowerNumber = 0;		//定义落子的左下连续相同的个数
        int rightUpperNumber = 0;		//定义落子的右上连续相同的个数
        int rightLowerNumber = 0;		//定义落子的右下连续相同的个数
        for(int i=1;point1-i>0;i++) {   	//往左边数有多少个是连续相同的
            if(str[point2][point1]==str[point2][point1-i]) {
                leftNumber++;
            }else {
                break;
            }
        }
        for(int i=1;point1+i<str.length;i++) {   	//往右边数有多少个是连续相同的
            if(str[point2][point1]==str[point2][point1+i]) {
                rightNumber++;
            }else {
                break;
            }
        }
        for(int i=1;point2-i>0;i++) {   	//往上边数有多少个是连续相同的
            if(str[point2][point1]==str[point2-i][point1]) {
                upperNumber++;
            }else {
                break;
            }
        }
        for(int i=1;point2+i<str.length;i++) {   	//往下边数有多少个是连续相同的
            if(str[point2][point1]==str[point2+i][point1]) {
                lowerNumber++;
            }else {
                break;
            }
        }
        for(int i=1;point1-i>0 && point2-i>0;i++) {   	//往左上边数有多少个是连续相同的
            if(str[point2][point1]==str[point2-i][point1-i]) {
                leftUpperNumber++;
            }else {
                break;
            }
        }
        for(int i=1;point1+i<str.length && point2+i<str.length;i++) {   	//往右下数有多少个是连续相同的
            if(str[point2][point1]==str[point2+i][point1+i]) {
                rightLowerNumber++;
            }else {
                break;
            }
        }
        for(int i=1;point1-i>0 && point2+i<str.length;i++) {   	//往左下边数有多少个是连续相同的
            if(str[point2][point1]==str[point2-i][point1-i]) {
                leftLowerNumber++;
            }else {
                break;
            }
        }
        for(int i=1;point1+i<str.length && point2-i>0;i++) {   	//往右上数有多少个是连续相同的
            if(str[point2][point1]==str[point2+i][point1+i]) {
                rightUpperNumber++;
            }else {
                break;
            }
        }
        //判断
        if(leftNumber+rightNumber==4 || upperNumber+lowerNumber==4 || leftUpperNumber+rightLowerNumber==4 || leftLowerNumber+rightUpperNumber==4) {
            return false;
        }else {
            return true;
        }
    }
}
